#import "NSManagedObject+SRAdditions.h"
#import "NSManagedObjectContext+SRAdditions.h"

@implementation NSManagedObject (SRAdditions)

-(NSURL *)URIRepresentation {
    return [[self objectID] URIRepresentation];
}

-(NSString *)entityName {
    return [[self entity] name];
}

-(BOOL)canContainObjectsWithEntityNames:(NSArray *)entityNames {
    return NO;
}

-(void)initializeWithDictionary:(NSDictionary *)dictionaryRepresentation {
    for (NSString *key in dictionaryRepresentation) {
        if (![self respondsToSelector:NSSelectorFromString(key)])
            continue;
        id object = [dictionaryRepresentation valueForKey:key];
        
        if ([object isKindOfClass:[NSArray class]] || [object isKindOfClass:[NSSet class]]) {
            id collection = [object isKindOfClass:[NSArray class]] ? [NSMutableArray array] : [NSMutableSet set];
            for (id collectionObject in object) {
                if ([collectionObject isKindOfClass:[NSURL class]])
                    collectionObject = [[self managedObjectContext] objectWithURI:collectionObject];
                [collection addObject:collectionObject];
            }
            object = collection;
        }
        if ([object isKindOfClass:[NSURL class]])
            object = [[self managedObjectContext] objectWithURI:object];
        if (object == [NSNull null])
            continue;
        [self setValue:object forKey:key];
    }
}

-(void)moveToContainer:(NSManagedObject *)container {
    // Default does nothing, subclasses implement this
}


@end
